﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.PickCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    /// <summary>
    /// 生产领料
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class PickController : ControllerBase
    {



        IMediator _mediator;


        public PickController(IMediator mediator)
        {
            _mediator = mediator;
        }



        /// <summary>
        /// 新增生产领料信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPickInfo(AddPickingDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();


            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                command.AddName = name;
                result.Data = await _mediator.Send(new CreatePickCommand { AddPickDtos = command });
                result.Code = 200;
                result.Message = "新增生产领料信息成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// //查询生产领料信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectPickDTO>>> ShowPickInfo([FromQuery] SelectPickCommand command)
        {
            ApiResult<PageDTO<SelectPickDTO>> result = new ApiResult<PageDTO<SelectPickDTO>>();


            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "查询生产领料信息成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }


        /// <summary>
        /// 删除生产领料信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> BatchDelPicking(BatchDelPickCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "删除生产领料信息成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 领料信息回填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FillPickDTO>> BackFillPick([FromQuery] FillPickCommand command)
        {
            ApiResult<FillPickDTO> result = new ApiResult<FillPickDTO>();

            try
            {
                result.Data = await _mediator.Send(command);
                result.Code = 200;
                result.Message = "领料信息回填成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 更新生产领料信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UptPicking(UptPickDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new UptPickCommand { UptDTO = command });
                result.Code = 200;
                result.Message = "更新生产领料信息成功";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
